package com.tangtang.gulimall.product.service.impl;

import com.tangtang.gulimall.product.Vo.AttrWithattrVo;
import com.tangtang.gulimall.product.dao.AttrDao;
import com.tangtang.gulimall.product.entity.AttrEntity;
import com.tangtang.gulimall.product.service.AttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangtang.common.utils.PageUtils;
import com.tangtang.common.utils.Query;

import com.tangtang.gulimall.product.dao.AttrGroupDao;
import com.tangtang.gulimall.product.entity.AttrGroupEntity;
import com.tangtang.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catagroyid) {
        //看出传过来的key是否为空,先进行这个的判断
        String key = (String) params.get("key");
        //先将mybatisplus的查询写法放在这里
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        //如果key不等于空
        //String key = (String) params.get("key");
        //select * FROM pms_attr_group where catelog_id = ? and (attr_group_id = key OR attr_group_name like key)
        if(!StringUtils.isEmpty(key))
        {
            //执行and查询
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if(catagroyid == 0)
        {
            //传来的catagroyid不是三级id,就进行全部查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page);
        }else {
            //否则就根据传来的三级id进行查询相应的attr_group表中对应的id属性（应该就是sku或者spu）
            queryWrapper.eq("catelog_id",catagroyid);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                        //这里可以直接调用
                        queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrWithattrVo> getallattrWithattr(Long catelogId) {
        List<AttrWithattrVo> attrWithattrVoList = new ArrayList<AttrWithattrVo>();
        //根据分类id查询所有的attr_group中的所有数据
        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId)
        );
        for (AttrGroupEntity attrGroupEntity:attrGroupEntities) {
            AttrWithattrVo attrWithattrVo = new AttrWithattrVo();
            BeanUtils.copyProperties(attrGroupEntity,attrWithattrVo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
            attrWithattrVo.setAttrs(attrEntities);
            attrWithattrVoList.add(attrWithattrVo);
        }
        return attrWithattrVoList;
        /*//1、查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrWithattrVo> collect = attrGroupEntities.stream().map(group -> {
            AttrWithattrVo attrsVo = new AttrWithattrVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;*/
    }

}